package com.common.supply.config.exception;

import com.common.supply.Utils.Func;
import com.common.supply.common.RspEnmu;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @Author: Jamnsin
 * @Date: 2019/12/26 4:27 下午
 */
@Slf4j
@Data
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private Integer code;

    private String message;

    private String details;

    private String resStr;

    public BusinessException(String message) {
        this.message = message;
    }

    public BusinessException(String message, Map msgParams) {
        for (Object key : msgParams.keySet()) {
            String param = Func.isEmpty(msgParams.get(key)) ? "null" : msgParams.get(key).toString();
            message = message.replaceAll("#" + key + "#", param);
        }
        this.message = message;
    }

    public BusinessException(String message, Integer code) {
        this.message = message;
        this.code = code;
    }

    public BusinessException(Throwable e, Integer code) {
        super(e);
        this.code = code;
    }

    public BusinessException(String message, Throwable e) {
        super(message, e);
        log.error("系统异常：", e);
        this.message = message;
    }

    public BusinessException(RspEnmu rspEnmu, Throwable e) {
        super(rspEnmu.getRetMsg(), e);
        log.error("系统异常：", e);
        this.code = Integer.valueOf(rspEnmu.getRetCode());
        this.message = rspEnmu.getRetMsg();
    }

    public BusinessException(RspEnmu rspEnmu, String message) {
        super(rspEnmu.getRetMsg(), new Throwable(message));
        this.code = Integer.valueOf(rspEnmu.getRetCode());
        this.message = rspEnmu.getRetMsg();
    }

    public BusinessException(String details, RspEnmu rspEnmu) {
        this.code = Integer.valueOf(rspEnmu.getRetCode());
        this.message = rspEnmu.getRetMsg();
        this.details = details;
    }

    public BusinessException(String details, String resStr, RspEnmu rspEnmu) {
        this.code = Integer.valueOf(rspEnmu.getRetCode());
        this.message = rspEnmu.getRetMsg();
        this.details = details;
        this.resStr = resStr;
    }

    public BusinessException(RspEnmu rspEnmu) {
        this.code = Integer.valueOf(rspEnmu.getRetCode());
        this.message = rspEnmu.getRetMsg();
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }
}
